package com.ccit.controller;
import com.ccit.domain.AttendanceType;
import com.ccit.domain.Employee;
import com.ccit.domain.Time;
import com.ccit.domain.User;
import com.ccit.service.EmployeeService;
import com.ccit.service.UserService;
import com.ccit.utils.Code;
import com.ccit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.util.UUID;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
@CrossOrigin(origins = "http://localhost:8080")
@Api(description = "管理")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;


    @GetMapping
    public Result selectAll(){
        List<User> userList = userService.selectAll();
        Integer code = userList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = userList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,userList,msg);
    }

    @GetMapping("/get_time")
    @ApiOperation(value = "上班时间设定")
    public Result getTime(){
        List<Time> timeList = userService.getTime();
        Integer code = timeList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = timeList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,timeList,msg);
    }


    @PostMapping("/login")
    public Employee login(@RequestBody Map<String, String> credentials) {
        //使用 Map<String, String> 来接收请求体中的 JSON 数据
        String username = credentials.get("username");
        String password = credentials.get("password");
        User user = userService.login(username, password);
        if (user != null) {
            // TODO: 根据 id 查询 employee 信息
            Integer id = user.getEmployeeId();
            Employee employee = employeeService.getById(id);
            return employee;
        } else {
            return null;
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody Map<String, String> credentials) {

        String employeeIdString = credentials.get("employeeId");
        Integer employeeId = Integer.parseInt(employeeIdString);
        String username = credentials.get("username");
        String password = credentials.get("password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmployeeId(employeeId);
        System.out.println(user);

//        // TODO: 检查用户名是否已经被注册
        if (userService.isUsernameTaken(username)) {
            return null; // 用户名已被注册，返回null表示注册失败
        }
        boolean flag = userService.add(user);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    



//    @ApiOperation(value = "头像上传")
//    @PostMapping("/fileUpload")
//    public Result fileUpload(@RequestParam(value = "file") MultipartFile file) {
//        if (file.isEmpty()) {
//            System.out.println("文件为空");
//            return Result.error("文件为空");
//        }
//        try {
//            String fileName = file.getOriginalFilename(); // 获取文件名
//            String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 获取文件后缀名
//            String filePath = "D://EduOl_File/kq/"; // 上传后的路径，可以改为配置文件中的属性
//            fileName = UUID.randomUUID().toString() + "_" + System.currentTimeMillis() + suffixName; // 生成新文件名，增加时间戳
//            File dest = new File(filePath + fileName); // 根据路径和文件名创建目标文件
//
//            if (!dest.getParentFile().exists()) {
//                boolean created = dest.getParentFile().mkdirs(); // 如果目录不存在，则创建目录
//                if (!created) {
//                    return Result.error("无法创建文件路径");
//                }
//            }
//            file.transferTo(dest); // 将上传的文件保存到目标文件中
//
//            String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
//                    .path("image/" + fileName)
//                    .toUriString(); // 构建文件下载的URI
//
//            return Result.success(fileDownloadUri); // 返回成功响应，包含文件下载的URI
//        } catch (IOException e) {
//            e.printStackTrace();
//            return Result.error("文件上传失败"); // 发生异常时返回失败响应
//        }
//    }

}
